import { _decorator, Component, game, Label, Node, Sprite, SpriteFrame } from 'cc';
import { HttpCom } from '../net/HttpCom';
import { GlobalVar } from '../GlobalVar';
import { GameEvent } from '../EventName';
import { ModelWeapon } from './ModelWeapon';
const { ccclass, property } = _decorator;

@ccclass('WeaponFactory')
export class WeaponFactory extends Component {
    @property(Node)
    node1: Node = null;
    @property(Node)
    node2: Node = null;
    @property(Node)
    node3: Node = null;
    @property(Label)
    nowLevel: Label = null;
    @property(Label)
    nextLevel: Label = null;
    @property(Node)
    jinbiLayout: Node = null;
    @property(Node)
    tongLayout: Node = null;
    @property(Node)
    jinLayout: Node = null;
    @property(Node)
    unlockLabelLayout: Node = null;
    @property(SpriteFrame)
    weaponSRSpriteF: SpriteFrame[] = [];
    @property(Sprite)
    weaponSRIcon: Sprite = null;
    @property(Label)
    damageLabel: Label = null;
    @property(Label)
    damageBonusLabel: Label = null;
    @property(Label)
    nameLabel: Label = null;//玩家名字
    @property(Label)
    idLabel: Label = null;//玩家ID
    @property(Label)
    jinbiLabel: Label = null;
    @property(Label)
    zuanLabel: Label = null;
    @property(SpriteFrame)
    allweaponSprite: SpriteFrame[] = [];
    @property(Sprite)
    weaponNode: Sprite = null;

    protected onLoad(): void {

    }
    initView() {
        this.nameLabel.string = GlobalVar.userInfo.username;
        this.idLabel.string = "ID:" + GlobalVar.userInfo.id;
        this.updateCoinLabels();
        HttpCom.getWeapon((data) => {
            if (data.data.isLock == 0) {
                this.node2.active = false;
                this.updateWeapon();
            } else {
                this.node2.active = true;
                this.node1.active = false;
                this.node3.active = false;
                this.getUnlockPrice();
            }
        })
    }
    private updateCoinLabels() {
        this.jinbiLabel.string = String(GlobalVar.userInfo.walletList[0].normalBalance);
        this.zuanLabel.string = String(GlobalVar.userInfo.walletList[3].normalBalance);
    }
    /**更新当前的武器页面 */
    private updateWeapon() {
        this.node1.active = true;
        this.node3.active = false;
        HttpCom.upgradeInfo((data) => {
            this.nowLevel.string = "level" + data.data.level;
            this.nextLevel.string = "level" + (data.data.level + 1);
            let index = data.data.level - 1;
            if (data.data.grade == "SSR") {
                index += 10;
            }
            index = index < 0 ? 0 : index;
            index = index >= this.allweaponSprite.length ? this.allweaponSprite.length - 1 : index;
            this.weaponNode.spriteFrame = this.allweaponSprite[index];
            this.damageLabel.string = data.data.damage;
            this.damageBonusLabel.string = "+" + data.data.damageBonus + "/s";
            if (data.data.grade == "SR") {
                this.weaponSRIcon.spriteFrame = this.weaponSRSpriteF[0];
            } else if (data.data.grade == "SSR") {
                this.weaponSRIcon.spriteFrame = this.weaponSRSpriteF[1];
            }
            this.jinbiLayout.getChildByName("need").getComponent(Label).string = "0";
            this.tongLayout.getChildByName("need").getComponent(Label).string = "0";
            this.jinLayout.getChildByName("need").getComponent(Label).string = "0";
            this.jinbiLayout.getChildByName("now").getComponent(Label).string = "/" + ModelWeapon.getInstance().formatBytes(GlobalVar.userInfo.walletList[0].normalBalance);
            this.tongLayout.getChildByName("now").getComponent(Label).string = "/" + ModelWeapon.getInstance().formatBytes(GlobalVar.userInfo.walletList[2].normalBalance);
            this.jinLayout.getChildByName("now").getComponent(Label).string = "/" + ModelWeapon.getInstance().formatBytes(GlobalVar.userInfo.walletList[4].normalBalance);
            let list = data.data.coinList;
            for (let i = 0; i < list.length; i++) {
                if (list[i].name == "GOLD") {
                    this.jinbiLayout.getChildByName("need").getComponent(Label).string = list[i].value;
                } else if (list[i].name == "COPPER_ORE") {
                    this.tongLayout.getChildByName("need").getComponent(Label).string = list[i].value;
                } else if (list[i].name == "GOLD_ORE") {
                    this.jinLayout.getChildByName("need").getComponent(Label).string = list[i].value;
                }
            }
        })
    }
    /**获取武器解锁需要的金币 */
    private getUnlockPrice() {
        HttpCom.getUnlockPrice((data) => {
            this.unlockLabelLayout.getChildByName("need").getComponent(Label).string = data.data;
            this.unlockLabelLayout.getChildByName("now").getComponent(Label).string = "/" + GlobalVar.userInfo.walletList[0].normalBalance;
        })
    }
    /**武器升级 */
    public upgradeWeapon() {
        HttpCom.upgradeWeapon((data) => {
            if (data.code == 200) {
                HttpCom.getUserInfo(() => {
                    this.updateCoinLabels();
                    this.updateWeapon();
                })
            } else if (data.code == 500) {
                game.emit(GameEvent.SHOW_TOAST, data.msg)
            }
        })
    }
    /**解锁武器 */
    public unlock() {
        HttpCom.unlock((data) => {
            if (data.code == 200) {
                HttpCom.getUserInfo(() => {
                    this.initView()
                })
            } else if (data.code == 500) {
                game.emit(GameEvent.SHOW_TOAST, data.msg)
            }
        })
    }

    start() {

    }

    update(deltaTime: number) {

    }
}


